import json
import os
import random
import time

import requests

# 获取当前 Python 文件的路径
current_dir = os.path.dirname(os.path.abspath(__file__))

# 指定列表 JSON 文件所在的文件夹路径
folder_path = 'd:\\Project\\Python\\fang\\resource\\list_json'

# 创建保存结果的文件夹
result_folder = 'd:\\Project\\Python\\fang\\resource\\detail_json'

# 自定义请求头
headers = {
    'accept': 'application/json, text/javascript, */*; q=0.01',
    'request-from': '1',
    'Origin': 'https://erp.fangline.cn',
    'x-requested-with': 'XMLHttpRequest',
    'page-name': '/visit-house/house-list',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
    'cookie': 'SERVERID=d1ea9c9d1b92967eba818ea87fabcbc7|1719655209|1719655205;Path=/; JSESSIONID=6473ec02-479f-437e-8882-8dc426f35308; SERVERID=d1ea9c9d1b92967eba818ea87fabcbc7|1719655301|1719655205; guideStatus=6',
    'Cache-Control': 'no-cache',
    'Host': 'erp.fangline.cn',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    'Content-Length': '0'
}

os.makedirs(result_folder, exist_ok=True)

for filename in os.listdir(folder_path):
    """循环读取文件夹中的 JSON 文件
    """
    if filename.endswith('.json'):
        file_path = os.path.join(folder_path, filename)

        # 读取文件中的 JSON 数据
        with open(file_path, 'r', encoding='utf-8') as file:
            json_data = json.load(file)
            
        print(f'读取文件{file_path}')

        # 获取 records 集合
        records = json_data['page']['records']

        # 循环集合并发送请求
        for record in records:
            try:
                house_id = record['houseId']
                result_filename = f'{result_folder}\\{house_id}.json'

                # 判断文件是否已存在
                if os.path.exists(result_filename):
                    print(f'File for houseId {house_id} already exists. Skipping...')
                    continue
        
                url = f'http://erp.fangline.cn/house/get-house-details-by-id.json?houseId={house_id}'

                # 发送 HTTP 请求
                response = requests.post(url=url, headers=headers)

                # 创建保存结果的文件名
                result_filename = f'{result_folder}/{house_id}.json'

                # 将响应结果保存到文件
                with open(result_filename, 'w') as result_file:
                    result_file.write(response.text)
                
                sleep_time = random.randint(1, 3)

                # 打印保存结果的文件路径
                print(f'保存房源至{result_filename}完成, 停顿{sleep_time}秒')

                # 睡眠 10 秒钟
                # time.sleep(1)
            except Exception as e:
                print(e)
            
